﻿using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace mozhi.smarterp.Business.EntityFrameworkCore;

public class BusinessHttpApiHostMigrationsDbContextFactory : IDesignTimeDbContextFactory<BusinessHttpApiHostMigrationsDbContext>
{
    public BusinessHttpApiHostMigrationsDbContext CreateDbContext(string[] args)
    {
        var configuration = BuildConfiguration();

        var builder = new DbContextOptionsBuilder<BusinessHttpApiHostMigrationsDbContext>()
            .UseMySql(configuration.GetConnectionString("Business"), ServerVersion.Parse("8.0.31-mysql"));

        return new BusinessHttpApiHostMigrationsDbContext(builder.Options);
    }

    private static IConfigurationRoot BuildConfiguration()
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false);

        return builder.Build();
    }
}
